package com.sl.proxy.server.controller;


import com.sl.proxy.server.base.page.PageInfo;
import com.sl.proxy.server.base.page.PageQuery;
import com.sl.proxy.server.controller.req.system.PortGroupCreateReq;
import com.sl.proxy.server.controller.req.system.PortGroupDeleteReq;
import com.sl.proxy.server.controller.req.system.PortGroupListReq;
import com.sl.proxy.server.controller.req.system.PortGroupUpdateEnableStatusReq;
import com.sl.proxy.server.controller.res.system.PortGroupCreateRes;
import com.sl.proxy.server.controller.res.system.PortGroupListRes;
import com.sl.proxy.server.controller.res.system.PortGroupUpdateEnableStatusRes;
import com.sl.proxy.server.service.PortGroupService;
import com.sl.proxy.server.util.ParamCheckUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 端口分组控制层
 */
@RequestMapping("/port-group")
@RestController
@RequiredArgsConstructor
public class PortGroupController {

    private final PortGroupService portGroupService;

    @PostMapping("/create")
    public PortGroupCreateRes create(@RequestBody PortGroupCreateReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotEmpty(req.getName(), "name");
        ParamCheckUtil.checkNotNull(req.getPossessorType(), "possessorType");
        ParamCheckUtil.checkNotNull(req.getPossessorId(), "possessorId");

        return portGroupService.create(req);
    }

    @GetMapping("/page")
    public PageInfo<PortGroupListRes> page(PageQuery pageQuery, PortGroupListReq req) {
        ParamCheckUtil.checkNotNull(pageQuery, "pageQuery");
        return portGroupService.page(pageQuery, req);
    }

    @GetMapping("/list")
    public List<PortGroupListRes> list(PortGroupListReq req) {
        return portGroupService.list(req);
    }

    @PostMapping("/update/enable-status")
    public PortGroupUpdateEnableStatusRes updateEnableStatus(@RequestBody PortGroupUpdateEnableStatusReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getId(), "id");
        ParamCheckUtil.checkNotNull(req.getEnable(), "enable");

        return portGroupService.updateEnableStatus(req);
    }

    @PostMapping("/delete")
    public void delete(@RequestBody PortGroupDeleteReq req) {
        ParamCheckUtil.checkNotNull(req, "req");
        ParamCheckUtil.checkNotNull(req.getId(), "id");

        portGroupService.delete(req.getId());
    }


}
